package com.yanqu.road.server.cmd.chefduel.process2;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.chefduel.ChefDuelProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.chefduel.CrossChefDuelMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.List;


@Cmd(code = Protocol.C_CROSS_CHEF_DUEL2_JOIN_TABLE, desc = "参加宴会")
public class CrossJoinTableCmd extends CrossCommand {

    private int activityId;
    private long homeUserId;
    private long joinUserId;
    private int tableId;
    private int spoonId;
    //玩家自己范围内的活动
    private int trueActivityId;
    private int campId;
    private UserBaseInfo userBaseInfo;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ChefDuelProto.ChefDuel2JoinTableReqMsg reqMsg = ChefDuelProto.ChefDuel2JoinTableReqMsg.parseFrom(packet.getBytes());
        joinUserId = packet.getPlayerId();
        activityId = reqMsg.getActivityId();
        homeUserId = reqMsg.getUserId();
        tableId = reqMsg.getTableId();
        spoonId = reqMsg.getSpoonId();
        if(reqMsg.hasOtherActivityId()){
            trueActivityId = reqMsg.getOtherActivityId();
        }
        campId = reqMsg.getCampId();
        if(reqMsg.hasUserBaseInfo()){
            userBaseInfo = PlayerBasePb.parseToUserBaseInfo(reqMsg.getUserBaseInfo());
        }
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        ret = CrossChefDuelMgr.joinTable(activityId,serverId,joinUserId,homeUserId,tableId,spoonId, trueActivityId, campId);
        if(ret == 0 && userBaseInfo != null){
            CrossUserMgr.updateUserBaseInfo(joinUserId, userBaseInfo);
        }
    }

    @Override
    public void sendData() {
        if(ret != 0){
            List<Integer> fspoonIdList = CrossChefDuelMgr.getUserForbiddenSpoonIdList(activityId,homeUserId);
            ChefDuelProto.CrossChefDuel2JoinTableMsg.Builder msg = ChefDuelProto.CrossChefDuel2JoinTableMsg.newBuilder();
            msg.setRet(ret);
            msg.setActivityId(activityId);
            msg.setJoinerUserId(joinUserId);
            msg.setHomeUserId(homeUserId);
            msg.setTableId(tableId);
            msg.setSpoonId(spoonId);
            if(trueActivityId > 0){
                msg.setActivityId(trueActivityId);
            }
            msg.addAllForbidSpoonId(fspoonIdList);
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_CHEF_DUEL2_JOIN_JOINER, msg);
            MessageHelper.sendPacket(serverId, joinUserId, pbMsg);
        }
    }

}
